var salt = sessionStorage.getItem('usersalt');

var app = new Vue({
    el: ".tabsty",
    data: {
        list: [],
        baseurl:url,
    },
    updated: function () {
        var swiper = new Swiper('.swiper-container', {
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
            pagination: {
                el: '.swiper-pagination',
            },
        });
        layer.photos({
            photos: '#layer-photos-demo'
            ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
        });
    }
})


layui.use(['form', 'layer', 'laytpl', 'carousel'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laytpl = layui.laytpl,
		carousel = layui.carousel;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}
	
	//下载
	$(".download_btn").click(function(){
		window.open(url + '/attachment/seekFile/' + course.attId);
	});
	
	//全屏
	$(".allscreen_btn").click(function(){
		
	});

});

 $.post(url + "/baseUsers/getInfo",{Token:salt},function (data) {
        var data = data.data;
        $("#userName").html(data.username);
    })
function studyTime() {
    $.post(url+"/course/studyTime",{Token: salt, id : courseId},function (data) {
    })
}


function setTableHeight(){
    var bodyHeight = $("body").height();
    var tabHeight = $(".topsty").height();
    var firLineHeight = $("#firLine").height();
    $("#secLine").height(bodyHeight - tabHeight - firLineHeight - 40);
}
setTableHeight();

var carouselIndex;
function getCourseInfo(){
    $.ajax({
        url: url + "course/getInfo",
        dataType: "json",
        async: true,
        type: "post",
        data: {
            Token: salt,
            id : courseId
        },
        success: function(data){
            if (data.code == 0) {
                course = data.data;
                $("#courseName").html(course.name);
                //$("#userName").html(course.userName);

                // var record = {url : "",list : []};
                // record.url = url;
                // record.list = course.imgIds.split(",");
                //渲染课程列表html
                // var jSliderHtml = $("#jSliderHtml").html();
                // laytpl(jSliderHtml).render(record, function(html){
                //   	$("#jSliderViewer").html(html);
                //
                //   	var width = $("#secLine").width();
                //   	var height = $("#secLine").height();
                //   	carouselIndex = carousel.render({
                // 	    elem: "#jSliderViewer",
                // 	    width: width + "px", //设置容器宽度
                // 	    height: height + "px", //设置容器高度
                // 	    autoplay: false, //不自动切换
                // 	    arrow: "always", //始终显示箭头
                // 	    indicator: "none"
                // 	});
                // });
                $.post(url + "attachment/changePptToImg",{id:course.attId,Token: salt,},function (data) {
                    if(data.code == 0){
                        var data = data.data;
                        app.list = data;
                    }
                })
                // console.log(url + "file/" + course.videoUrl)
                studyTime()
            } else {
                setTimeout(function(){
                    layer.msg(data.msg);
                }, 1000);
            }
        },
        error: function(error){
        }
    });
}

var course;
var courseId = getQueryVariable("id");
if (courseId != false) {
    getCourseInfo();
} else {
    layer.msg("参数id不存在");
    setTimeout(function(){
        history.go(-1);
    }, 500);
}

/*------------------页面计时.start------------------*/
var monitor_plus = {
    click_time : new Date(),
    leave : 180,//3分钟页面未操作时间
    timer : true,//是否学习计时
    pageTime : 180,//本页3分钟学习时间
    studyTime : 0,//一共学习时间
    init : function(){
        studyTime()
        clearTimeout(timeOut);
        this.timer = true;
        this.pageTime = 180;
        $("#remainTime").removeClass("layui-hide").addClass("layui-show");
        $("#continueTime").removeClass("layui-show").addClass("layui-hide");
        this.countimer();
    },
    countimer : function() {
        var now = new Date();
        var span = parseInt(now - this.click_time) / 1000;
        if (span > this.leave) {
            //3分钟无操作弹出继续计时按钮
            this.timer = false;
            var layerIndex = layer.confirm("继续学习", {
                icon: 3,
                title: false,
                closeBtn: 0,
                btn: ["确定"]
            }, function(){
                monitor_plus.timer = true;
                layer.close(layerIndex);
                monitor_plus.click_time = new Date();
                monitor_plus.countimer();
            });
        } else {
            if (this.pageTime == 0) {
                this.timer = false;
                $("#remainTime").removeClass("layui-show").addClass("layui-hide");
                $("#continueTime").removeClass("layui-hide").addClass("layui-show");
                $(".tddlabbg2").css("width", "100%");
                $(".tddlabbg3").css("width", "0%");
            }

            timeOut = setTimeout(function(){
                monitor_plus.countimer();
            }, 1000);
        }
        if (this.timer == true) {
            this.pageTime--;
            this.studyTime++;

            //学习时间显示
            var html = changeSecondShow(this.studyTime);
            $("#studyTime").html(html);

            //本页剩余时间显示
            html = changeSecondShow(this.pageTime);
            $("#pageRemainTime").html(html);

            //进度条显示
            var percent2 = numMulti(numDiv(this.pageTime, 180), 100);
            var percent3 = numSub(100, percent2);
            $(".tddlabbg2").css("width", percent2 + "%");
            $(".tddlabbg3").css("width", percent3 + "%");
        }
    },
    update : function() {
        this.click_time = new Date();
    }
};

$(document).ready(function() {
	/* 鼠标移动事件 */
    $(document).mouseover(function(){
        monitor_plus.update(); //更新操作时间
    });
    monitor_plus.countimer(); //更新操作时间
});

/*------------------页面计时.end------------------*/

//秒数转换成时分秒
function changeSecondShow(secondTotal){
    var hour = parseInt(numDiv(secondTotal, 3600));
    var minute = parseInt(numDiv(secondTotal, 60));
    var second = secondTotal % 60;
    var html = "";
    if (hour > 0) {
        html = hour + "h";
    }
    if (minute > 0) {
        html += minute + "min";
    }
    html += second + "s";
    return html;
}